package com.rabbit2020.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 作者: LCG
 * 日期: 2020/2/26 00:24
 * 描述:
 */
@Configuration
public class RabbitMqConfig {



    /**
     * 定义一个fanout交换器
     * @return
     */
   /* @Bean
    public Exchange getExchange(){

        return ExchangeBuilder.fanoutExchange("test-fanout-exchange").durable(false).autoDelete().build();
    }*/





    //配置死信交换器
    @Bean("deadExchange")
    public Exchange getDeadExchange(){
        return ExchangeBuilder.directExchange("SI-XIN-EXCHANGE").durable(true).build();
    }

    //创建死信队列
    @Bean("deadQueue")
    public Queue getDeadQueue(){
       return new Queue("SI-XIN-QUEUE",true,false,false,null);
    }


    //创建一个死信的绑定
    @Bean("deadBinding")
    public Binding getDeadBinding(Queue deadQueue,Exchange deadExchange){
        return BindingBuilder.bind(deadQueue).to(deadExchange).with("SI-XIN-KEY").noargs();
    }


    //创建一个普通的交换器
    @Bean("puTongExchange")
    public Exchange getPuTongExchange(){
        return ExchangeBuilder.directExchange("PU-TONG-EXCHANGE").durable(true).build();
    }

    //创建普通队列
    @Bean("puTongQueue")
    public Queue getPuTongQueue(){
        Map<String,Object> parm=new HashMap<>();
        parm.put("x-dead-letter-exchange","SI-XIN-EXCHANGE");
        parm.put("x-dead-letter-routing-key","SI-XIN-KEY");

        return new Queue("PU-TONG-QUEUE",true,false,false,parm);
    }

    //创建一个普通的绑定
    @Bean("puTongBinding")
    public Binding getPuTongBinding(Queue puTongQueue,Exchange puTongExchange){
        System.out.println(puTongQueue+"================"+puTongExchange);
       return BindingBuilder.bind(puTongQueue).to(puTongExchange).with("PU-TONG-KEY").noargs();
    }



}
